package com.mjw.zstu.controller;

import com.mjw.zstu.resultDto.JsonResult;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
public class FileController {
    @RequestMapping("file")
    public JsonResult<List> uploadFile(@RequestParam("file")MultipartFile[] file){
        List<String> urls=new ArrayList<>();
        String path="/Users/xiaoming/Pictures/image";
        for(int i=0;i<file.length;i++){
            long t=System.currentTimeMillis();
            System.out.println(file[i].getOriginalFilename());
            String[] s= StringUtils.split(file[i].getOriginalFilename(),".");
            System.out.println(file[i].getOriginalFilename());
            File ff=new File(path,String.valueOf(t)+"."+s[s.length-1]);
            try {
                file[i].transferTo(ff);
                urls.add("http://localhost:3001/image/"+String.valueOf(t)+"."+s[s.length-1]);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new JsonResult<>(200,"上传成功",urls);
    }
}
